﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;

using iSun.Entity;
using OpenExpressApp;
using iSun.Web.UI.Controls;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;


namespace ZX.WebApp.DesignForm
{
    public partial class index : BasePage
    {
        /// <summary>
        /// 表单ID
        /// </summary>
        public string FormID
        {
            get;
            set;
        }
        /// <summary>
        /// 表单名
        /// </summary>
        public string FormName
        {
            get;
            set;
        }
        /// <summary>
        /// 主表名
        /// </summary>
        public string MasterTableName
        {
            get;
            set;
        }
        /// <summary>
        /// 模板名
        /// </summary>
        public string TemplateName
        {
            get{return "autoUI";}
        }
        /// <summary>
        /// 主表JSON
        /// </summary>
        public string MasterJson
        {
            get;
            set;
        }
        /// <summary>
        /// 明细表JSON
        /// </summary>
        public string DetailsJson
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public string FieldItemsJson 
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //DateTime dt = DateTime.Now;
            string strAction = this.GetStringFromQueryString("action", "");
            string strFormID = this.GetStringFromQueryString("formid", "");
            string strEntity = this.GetStringFromQueryString("entity", "");
            string strTable = this.GetStringFromQueryString("table", "");
            string strFormName = this.GetStringFromQueryString("formname", "");
            string strJson = this.GetStringFromQueryString("json", "").Replace("[@@]", "\\");

            switch (strAction)
            {
                case "CreateForm":
                    //CreateForm(strFormName,strTable, strJson.Replace("[A]", "&"));
                    break;
                case "EditForm":
                    //InsertOrUpdate(strEntity, strJson.Replace("[A]", "&"), strAction);
                    EditForm(strFormID);
                    break;
                default:
                    break;

            }


            if (!this.IsPostBack)
            {
                //GetTableNames();
                //cblcols.Attributes.Add("onload ", "setTableName(); ");
            }
        }
        //private void CreateForm(string formName,string masterTableName,string strJson) 
        //{
        //    if (!string.IsNullOrEmpty(masterTableName))
        //    {
        //        FormID = Guid.NewGuid().ToString();
        //        FormName = formName;
        //        MasterTableName = masterTableName;
        //        //System.Data.DataTable dt = ZX.Common.Globals.Json2Dtb(strJson);
        //        HandlerService hs = new HandlerService();
        //        string formString = hs.CodeSmithCompleteString(FormName,MasterTableName, strJson);
        //        CKEditorDesignForm.Text = formString;
                
        //    }
        
        //}
        //private string ObjectListJSON(SysFieldItems Items)
        //{
        //    System.Text.StringBuilder sb = new System.Text.StringBuilder("");
        //    int i = 0;
        //    foreach (object item in Items)
        //    {
        //        i = i + 1;
        //        string str = (i == Items.Count) ? "" : ",";
        //        sb.Append(ZX.Common.Globals.objectToJSON(item) + str);
        //    }
        //    return sb.ToString();
        //}

        //public static string Dtb2Json(System.Data.DataTable dtb)
        //{
        //    JavaScriptSerializer jss = new JavaScriptSerializer();
        //    ArrayList dic = new ArrayList();
        //    foreach (System.Data.DataRow row in dtb.Rows)
        //    {
        //        Dictionary<string, object> drow = new Dictionary<string, object>();
        //        foreach (System.Data.DataColumn col in dtb.Columns)
        //        {
        //            drow.Add(col.ColumnName, row[col.ColumnName]);
        //        } dic.Add(drow);
        //    } return jss.Serialize(dic);
        //}

        public string ObjectListJSON(ArrayList Dic) 
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(Dic);
        }
        //public string ObjectListJSON(SysFieldItems Items)
        //{
           
            

        //    ArrayList dic = new ArrayList();
        //    foreach (object item in Items)
        //    {
        //        Dictionary<string, object> drow = new Dictionary<string, object>();

        //        Type t = item.GetType();
        //        System.Reflection.PropertyInfo[] Mypropertyinfos = t.GetProperties();
        //        foreach (System.Reflection.PropertyInfo pi in Mypropertyinfos)
        //        {
        //            if (pi.Name == "IsNew" || pi.Name == "IsDeleted" || pi.Name == "BrokenRulesCollection" || pi.Name == "IsDirty" || pi.Name == "IsSelfDirty" || pi.Name == "NeedCalc" || pi.Name == "IsSavable" || pi.Name == "IsValid" || pi.Name == "IsSelfValid" || pi.Name == "IsBusy" || pi.Name == "IsSelfBusy" || pi.Name == "IsObjectLazy" || pi.Name == "Parent")
        //            {  continue;}

        //                Object Value = t.InvokeMember(pi.Name,
        //                               System.Reflection.BindingFlags.DeclaredOnly |
        //                               System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |
        //                               System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, item, null);
        //                drow.Add(pi.Name, Value);
        //        }
                
        //        dic.Add(drow);
        //    }

        //    return Newtonsoft.Json.JsonConvert.SerializeObject(dic);
        //}

 
        private void EditForm(string formID)
        {
            if (!string.IsNullOrEmpty(formID))
            {
                SysCustomDefineForm form = new SysCustomDefineForm();
                Guid _FormID = new Guid(formID);
                form = SysCustomDefineForm.Get(_FormID);

                FormID = form.FormID.ToString();
                FormName = form.FormName;
                MasterTableName = form.MasterTableName;
                DetailsJson = form.DetailsJson;
               // IList<SysFieldItem> items = form.FieldItems.ToList<SysFieldItem>();
                //FieldItemsJson = ListToJson(items);
                CKEditorDesignForm.Text = form.HtmlString;
            }
        }
        ///// <summary>
        ///// 获取表名
        ///// </summary>
        //public void GetTableNames()
        //{
        //    SysCustomDefineForm df = new SysCustomDefineForm();
        //    IGTable it=df.getTableInfo();
        //    if (it.Count > 0) 
        //    {
        //        ddltable.DataSource = it.GetTable;
        //        ddltable.DataTextField = "TableName";
        //        ddltable.DataValueField = "TableName";
        //        ddltable.DataBind();
        //    }
        //}

        ///// <summary>
        ///// 下拉框变动时，置换表的字段
        ///// </summary>
        //protected void ddlColNum_OnSelectedIndexChanged(object sender, EventArgs e)
        //{
        //    DropDownList ddl = (DropDownList)sender;
        //    string table = ddl.SelectedValue;
        //    SysCustomDefineForm df = new SysCustomDefineForm();
        //    IGTable it = df.getTableField(table);
        //    if (it.Count > 0)
        //    {
        //        cblcols.DataSource = it.GetTable;
        //        cblcols.DataTextField = "ColDesc";
        //        cblcols.DataValueField = "ColName";
        //        cblcols.DataBind();
        //    }
        //}

        ///// <summary>
        ///// 绑定脚步事件onclick
        ///// </summary>
        //protected void cblcols_OnDataBound(object sender, EventArgs e)
        //{
        //    foreach (ListItem li in cblcols.Items)
        //    {
        //        li.Attributes.Add("onclick ", "setvalue(this); ");
        //    }
        //}
        
    }
}